﻿' Copyright © Microsoft Corporation.  All Rights Reserved.
' This code released under the terms of the 
' Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

Public Class RssRegion

#Region "Form Region Factory"

    <Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.PostRss)> _
    <Microsoft.Office.Tools.Outlook.FormRegionName("FormRegionOutlookAddIn.RssRegion")> _
    Partial Public Class RssRegionFactory

        ' Occurs before the form region is initialized.
        ' To prevent the form region from appearing, set e.Cancel to true.
        ' Use e.OutlookItem to get a reference to the current Outlook item.
        Private Sub RssRegionFactory_FormRegionInitializing(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs) Handles Me.FormRegionInitializing

        End Sub

    End Class

#End Region

    'Occurs before the form region is displayed. 
    'Use Me.OutlookItem to get a reference to the current Outlook item.
    'Use Me.OutlookFormRegion to get a reference to the form region.
    Private Sub RssRegion_FormRegionShowing(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.FormRegionShowing

        Me.RssRegionSplitContainer.Panel2Collapsed = True

        Dim rssItem As Outlook.PostItem = CType(Me.OutlookItem, Outlook.PostItem)

        Me.webBrowserRss.Navigate(Helper.ParseUrl(rssItem))

    End Sub

    'Occurs when the form region is closed.   
    'Use Me.OutlookItem to get a reference to the current Outlook item.
    'Use Me.OutlookFormRegion to get a reference to the form region.
    Private Sub RssRegion_FormRegionClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.FormRegionClosed

    End Sub

    ' Clicking on "SearchSimilarTopicsbutton" will open 'webBrowserSearch' in a Separate Pane.
    Private Sub searchSimilarTopicsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchSimilarTopicsButton.Click

        Dim rssItem As Outlook.PostItem = CType(Me.OutlookItem, Outlook.PostItem)

        Me.searchSimilarTopicsButton.Visible = False

        Me.RssRegionSplitContainer.Panel2Collapsed = False

        ' Search for the matching titles by placing title in "".
        Me.webBrowserSearch.Navigate(String.Concat("http://www.bing.com/search?q=""", rssItem.Subject, """"))

        Me.RssRegionSplitContainer.SplitterDistance = (Me.OutlookFormRegion.Inspector.Width / 2)

        Me.searchWindowProgressBar.Visible = True

    End Sub

    ' To navigate back to Rss Article with in 'View Article' Pane.
    Private Sub viewRssBackButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles viewRssBackButton.Click

        Me.webBrowserRss.GoBack()

    End Sub
    ' Event to set "ViewRssProgressBar" Properties.
    Private Sub webBrowserRss_Navigating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles webBrowserRss.Navigating

        Me.viewRssProgressBar.Visible = True

        Me.viewRssProgressBar.Value = 0

    End Sub

    Private Sub webBrowserRss_ProgressChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles webBrowserRss.ProgressChanged

        ' "ViewRssProgressBar.Visible" is set in Navigating and DocumentCompleted
        ' in order to keep it updated correctly, we Coordinate these three events

        If (Me.viewRssProgressBar.Visible) Then

            ' -1 Indicates download is completed
            If (e.CurrentProgress = -1) Then

                Me.viewRssProgressBar.Value = 100

            Else

                Me.viewRssProgressBar.Value = CType(((100 * e.CurrentProgress) / e.MaximumProgress), Integer)
            End If

        End If

    End Sub

    ' Event to set "ViewRssProgressBar" Properties.
    Private Sub webBrowserRss_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles webBrowserRss.DocumentCompleted

        Me.viewRssProgressBar.Visible = False

        Me.viewRssProgressBar.Value = 0

    End Sub

    ' Hide "Search Results" and expand "View Article" pane.
    Private Sub hideSearchResultsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hideSearchResultsButton.Click

        Me.RssRegionSplitContainer.Panel2Collapsed = True

        Me.searchSimilarTopicsButton.Visible = True

    End Sub

    ' To navigate back to Search Results.
    Private Sub searchResultsBackButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchResultsBackButton.Click

        Me.webBrowserSearch.GoBack()

    End Sub

    ' Event to set "Search ProgressBar" Properties.
    Private Sub webBrowserSearch_Navigating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles webBrowserSearch.Navigating

        Me.searchWindowProgressBar.Visible = True

        Me.searchWindowProgressBar.Value = 0

    End Sub

    Private Sub webBrowserSearch_ProgressChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles webBrowserSearch.ProgressChanged

        ' "searchWindowProgressBar.Visible" is set in Navigating and DocumentCompleted
        ' in order to keep it updated correctly, we Coordinate these three events.

        If (Me.searchWindowProgressBar.Visible) Then

            ' -1 Indicates download is completed.
            If (e.CurrentProgress = -1) Then

                Me.searchWindowProgressBar.Value = 100

            Else

                Me.searchWindowProgressBar.Value = CType(((100 * e.CurrentProgress) / e.MaximumProgress), Integer)
            End If

        End If

    End Sub

    ' Event used to set "ProgressBar" Properties.
    Private Sub webBrowserSearch_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles webBrowserSearch.DocumentCompleted

        Me.searchWindowProgressBar.Visible = False

        Me.searchWindowProgressBar.Value = 0

    End Sub

End Class
